import React, { useState, useEffect } from "react";
import "./App.scss";
import { useRoutes } from "react-router-dom";
import routes from "./routes";
import { useTranslation } from "react-i18next";
import { urlQuery } from "./utils";
import { ConfigProvider } from "antd";
import enUS from "antd/locale/en_US";
import zhCN from "antd/locale/zh_CN";
import dayjs from "dayjs";
import "dayjs/locale/zh-cn";
import { titleCheck } from "./utils";

function App() {
  const element = useRoutes(routes);

  const [locale, setLocal] = useState(zhCN);
  const { i18n } = useTranslation();
  useEffect(() => {
    // 挂载后或者更新后的操作
    const params_obj = urlQuery(window.location.href);
    console.log(params_obj);
    let url_company = params_obj["u"];
    let company = "";
    let session_company = sessionStorage.getItem("company");

    if (!session_company || (url_company && session_company !== url_company)) {
      company = url_company;
      sessionStorage.setItem("company", company);
    } else {
      company = session_company;
    }

    console.log("company:" + company);

    if (company === "mg") {
      dayjs.locale("en");
      setLocal(enUS);
      i18n.changeLanguage("en");
    } else {
      dayjs.locale("zh-cn");
      setLocal(zhCN);
      i18n.changeLanguage("zh");
    }

    document.title = titleCheck();
  }, []);

  return (
    <ConfigProvider locale={locale}>
      <div className="App">{element}</div>
    </ConfigProvider>
  );
}

export default App;
